<template>
  <div class="login-container">
    <!-- 左侧logo区域 -->
    <div class="logo" />
    <!-- 右侧表单区域 -->
    <div class="form">
      <h1>登录</h1>
      <el-card shadow="hover">
        <el-form ref="form" :model="loginForm" :rules="loginRules">
          <el-form-item prop="mobile">
            <el-input v-model.trim="loginForm.mobile" placeholder="请输入手机号" clearable></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model.trim="loginForm.password" placeholder="请输入密码" show-password clearable></el-input>
          </el-form-item>
          <el-form-item prop="isAgree">
            <el-checkbox v-model="loginForm.isAgree">
              本人已阅读并同意
            </el-checkbox>
            <el-link type="primary" href="https://www.12333.gov.cn/usercenter/register_info.shtml" target="_blank">《用户平台使用协议》</el-link>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="login">登录</el-button>
          </el-form-item>
          <el-button @click="testAxios">测试axios封装</el-button>
        </el-form>
      </el-card>
    </div>
  </div>
</template>
<script>
// import { login } from '@/api/user'
import request from '@/utils/request'

export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        mobile: process.env.NODE_ENV === 'development' ? '13800000002' : '',
        password: process.env.NODE_ENV === 'development' ? 'hm#qd@23!' : '',
        isAgree: process.env.NODE_ENV === 'development'
      },
      loginRules: {
        mobile: [{
          required: true,
          message: '手机号不能为空',
          trigger: 'blur'
        },
        {
          pattern: /^1[3-9]\d{9}$/,
          message: '手机号格式不正确',
          trigger: 'blur'
        }
        ],
        password: [{
          required: true,
          message: '密码不能为空',
          trigger: 'blur'
        },
        {
          pattern: /^.{6,12}$/,
          message: '密码由6~12位字符组成',
          trigger: 'blur'
        }
        ],
        isAgree: [
          {
            validator: (rule, value, callback) => {
              // rule: 校验规则
              // value: 待校验的值
              // callback() 函数: promise resolve reject
              // callback() 函数调用格式 ： callback(new Error(错误信息))
              value ? callback() : callback(new Error('您必须勾选平台用户的使用协议才能登录系统'))
            }
          }
        ]
      }
    }
  },
  methods: {
    login() {
      this.$refs.form.validate(async isOK => {
        if (isOK) {
          // alert('恭喜你，登录表单内容校验通过！')
          this.$message({
            message: '恭喜你，登录的表单内容校验通过！',
            type: 'success'
          })
          // Vuex中的action返回的是promise， 所以需要使用 async await
          await this.$store.dispatch('user/login', this.loginForm)
          // 若登录成功，则跳转到主页
          this.$router.push('/')
        } else {
          this.$message({
            message: '登录的表单内容校验未通过！',
            type: 'warning'
          })
        }
      })
    },
    testAxios() {
      request({
        url: '/sys/login',
        method: 'post',
        data: {
          mobile: '13800000002',
          password: 'hm#qd@23!'
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
  .login-container {
    display: flex;
    justify-content: center;
    align-content: center;
    height: 100vh;
    .logo {
      flex: 3;
      height: 100%;
      // background-color: skyblue;
      // background: url(../../assets/common/人力资源背景图2.jpg) no-repeat  center  center / contain;
      background: url(../../assets/common/人力资源背景图2.jpg) no-repeat  center  center / cover;
      border-top-right-radius: 60px;  // 设置右上角的弧度值
    }
    .form {
      flex: 2;
      height: 100%;
      background-color: rgb(233, 233, 240);
      padding-left: 100px;
      display: flex;
      flex-direction: column;
      justify-content: center;
      h1 {
        font-size: 32px;
        padding-left: 20px;
      }
      .el-card {
        border: 0;
        background-color: rgb(233, 233, 240);
        .el-input {
          width: 350px;
          height: 45px;
          .el-input__inner {    // 不生效。如果去掉 style 标签中的 scoped 属性可以生效，但不建议此法。待解决。
            background: #0b26bd;  //输入框的背景颜色
          }
        }
        .el-checkbox {
          color: rgb(235, 66, 66);
        }
        .el-button {
          width: 350px;
        }
      }
    }
  }
</style>
